package com.zhukunri.sp04.controller;


import com.zhukunri.sp01.domain.Item;
import com.zhukunri.sp01.domain.Order;
import com.zhukunri.sp01.domain.User;
import com.zhukunri.sp01.service.OrderService;
import com.zhukunri.sp01.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;


    //根据id获取订单
    @GetMapping("/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        //获取订单
        Order order = orderService.getOrder(orderId);
        //封装json
        JsonResult result = JsonResult.ok().data(order);

        return result;

    }


    //添加订单
    @GetMapping("/addOrder")
    public JsonResult<?> addOrder(){
        //写死一个订单数据
        Order order = new Order();
        order.setId("12s123");
        order.setUser(new User(8,null,null));
        ArrayList<Item> items = new ArrayList<>();
        items.add(new Item(1,"商品1",11));
        items.add(new Item(2,"商品2",22));
        items.add(new Item(3,"商品3",33));
        order.setItems(items);

        orderService.addOrder(order);

        //封装返回的json数据
        JsonResult result = JsonResult.ok().msg("添加订单成功");

        return result;

    }
}
